// AES_Algorithm.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <map>

using namespace std;

class Element
{
public:
	char upper;
	char lower;
	Element()
	{
		upper = '0';
		lower = '0';
	}

	Element(char a, char b)
	{
		upper = a;
		lower = b;
	}
};

class Lookup
{
public:
	Lookup();
	Lookup(char* fileName);
	Element find(char a, char b);

private:
	map<char, map<char, Element>> myMap;
};


class State
{
public:
	State();
	State(char* message);
	void printState();

	/*
	void subBytes();
	void invSubBytes();
	void shiftRows();
	void invShiftRows();
	void mixColumns();
	void invMixColumns();
	void addRoundKey();
	*/

private:
	map<int, map<int, Element>> myMap;	
};

int _tmain(int argc, _TCHAR* argv[])
{
	char* input = "00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF";

	Lookup myLookup("C:\\test\\Sbox.txt");

	Element myElement = myLookup.find('A', 'A');

	printf("First Number: %c Second Number %c", myElement.upper, myElement.lower);

	State myState(input);
	myState.printState();
	char x;
	cin >> x;
	return 0;
}

Lookup::Lookup()
{
	
}

Lookup::Lookup(char* fileName)
{
	map<char, Element> columnMap;
	ifstream myFile;
	Element x;
	char row, column;

	myFile.open(fileName, ios::in);

	if(myFile.is_open())
	{
		for(int i = 0; i <= 15; i++)
		{
			if(!myFile.eof())
			{
				row = myFile.get();
				myFile.get();

				for( int i = 0; i <= 15; i++ )
				{
					column = myFile.get();
					myFile.get();

					x.upper = myFile.get();
					x.lower = myFile.get();
					myFile.get();

					columnMap[column]=x;
				}

				myMap[row]=columnMap;
			}
		}		
	}
}

Element Lookup::find(char row, char column)
{
	return myMap.find(row)->second.find(column)->second;
}

State::State()
{

}

State::State(char* message)
{
	map<int, Element> columnMap;
	Element x;
	int k = 0;
	char a, b;

	for(int row = 0; row <= 3; row++)
	{
		for( int column = 0; column <= 3; column++)
		{
			a = message[k];
			k++;

			b = message[k];
			k++;
			k++;
			x.upper = a;
			x.lower = b;

			columnMap[column] = x;
		}

		myMap[row]=columnMap;
	}
}

void State::printState()
{
	map<int, Element> column;
	Element print;

	for(int i = 0; i <= 3; i++)
	{
		column = myMap[i];
		for( int j = 0; j <= 3; j++)
		{			
			print = column[j];
			printf("%c%c", print.upper, print.lower);
		}
	}
}
